package com.idconfirmsystem.parse;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringBufferInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.util.EncodingUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.idconfirmsystem.model.Task;
import com.idconfirmsystem.model.UserBean;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

/**
 * 用户登录返回数据解析
 * 
 * @author WuXiaoming (wuxm1011@163.com)
 * @data:
 * @version: V1.0
 */
public class LoginParse {

	List<Map<String, Object>> listDates = new ArrayList<Map<String, Object>>();
	int listLength;
	String TAG = "Login";
	UserBean userBean = UserBean.getInstance();

	public LoginParse(String result) {
		super();
		paeser(result);
	}

	public void setListDates(List<Map<String, Object>> listDate) {
		this.listDates = listDate;
	}

	public List<Map<String, Object>> getListDates() {
		return listDates;
	}

	public void setListLength(int length) {
		this.listLength = length;
	}

	public int getListLength() {
		return listLength;
	}

	public void paeser(String result) {
		// TODO Auto-generated method stub
		try {
			InputStream iStream = new ByteArrayInputStream(result.getBytes("GBK"));
			// result = EncodingUtils.getString(result.getBytes(), "GBK");
			// InputStreamReader isr = new InputStreamReader(iStream, "utf-8");
			// BufferedReader br = new BufferedReader(new InputStreamReader(
			// iStream, "UTF-8"));
			// InputSource is = new InputSource(br);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;
			db = dbf.newDocumentBuilder();
			Document document = db.parse(iStream);
			Element root = document.getDocumentElement();// 获得根元素
			System.out.println("根元素：" + root.getNodeName());
			// Log.e(TAG, "根元素Login==>：" + root.getNodeName());
			NodeList rowList = document.getElementsByTagName("row");
			// Log.e(TAG, "rowList: " + rowList.getLength());
			setListLength(rowList.getLength() - 9);
			for (int i = 5; i < rowList.getLength(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				NodeList info = rowList.item(i).getChildNodes();
				for (int j = 0; j < info.getLength(); j++) {
					// Log.e("Task", "info: " + info.getLength());
					Node infoNode = info.item(j);
					String nodesString = infoNode.getTextContent();
					// String nodesString =
					// infoNode.getTextContent();
					if ("aae314".equals(infoNode.getNodeName())) {
						Log.e(TAG, "aae314: " + nodesString);
						map.put("aae314", nodesString);
						userBean.setAae314(nodesString);
					} else if ("aae317".equals(infoNode.getNodeName())) {
						Log.e(TAG, "aae317: " + nodesString);
						map.put("aae317", nodesString);
						userBean.setAae317(nodesString);
					} else if ("system_user_real_name".equals(infoNode.getNodeName())) {
						Log.e(TAG, "system_user_real_name: " + nodesString);
						map.put("system_user_real_name", nodesString);
						userBean.setRealName(nodesString);
					} else if ("system_user_rights".equals(infoNode.getNodeName())) {
						map.put("system_user_rights", nodesString);
						String[] list = nodesString.split(",");
						Log.e(TAG, "system_user_rights: " + list.length + "|||" + list[0]);
						userBean.setUserLists(list);
					} else if ("branch_number".equals(infoNode.getNodeName())) {
						Log.e(TAG, "branch_number: " + nodesString);
						map.put("branch_number", nodesString);
						userBean.setBranchNumber(nodesString);
					}
				}
				listDates.add(map);
			}
			setListDates(listDates);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}